# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

# Base Docker image
FROM ubuntu:20.04

# ------------------------------------------------------------------------------
# Set Arguments and Environment Variable
ARG USERNAME
ARG USER_UID
ARG USER_GID=$USER_UID
ENV HOME /home/$USERNAME
ENV DEBIAN_FRONTEND=noninteractive
ENV LANG en_US.utf8
ENV LANGUAGE en_US.UTF-8
ENV LC_ALL en_US.UTF-8

# ------------------------------------------------------------------------------
# Install packages via apt-get
RUN set -x \
    && apt-get -y update \
    && apt-get -y install \
        bash-completion \
        bc \
        build-essential \
        bluez \
        bzip2 \
        clang \
        clang-tidy \
        cloc \
        curl \
        dbus \
        docker \
        docker.io \
        dos2unix \
        doxygen \
        expect \
        g++-7 \
        gcc \
        gettext \
        git \
        gpg \
        graphviz \
        gzip \
        iproute2 \
        iwyu \
        jq \
        lcov \
        libatomic1 \
        libncurses5 \
        libpython3.9-dev \
        libsm6 \
        libsndfile1 \
        libssl-dev \
        libxcursor1 \
        libxft2 \
        libxinerama1 \
        libxrandr2 \
        libxt6 \
        locales \
        mercurial \
        nano \
        net-tools \
        nfs-common \
        ninja-build \
        openssl \
        p7zip-full \
        perl \
        python-is-python3 \
        python3 \
        python3-dev \
        python3-pip \
        python3-venv \
        python3.9 \
        python3.9-dev \
        python3.9-venv \
        ruby-full \
        samba \
        software-properties-common \
        srecord \
        sudo \
        tar \
        unzip \
        util-linux \
        uuid-runtime \
        vim \
        wget \
        xmlstarlet \
        zip \
        libgirepository1.0-dev \
    && add-apt-repository universe \
    && curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py \
    && python3.9 get-pip.py \
    && update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1 \
    && localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8 \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists \
    && : # last line

# ------------------------------------------------------------------------------
# Install Python modules via pip3
RUN set -x \
    && pip3 install -U \
        GitPython \
        awscli \
        azure-identity \
        azure-iot-deviceupdate \
        azure-iot-hub \
        azure-storage-blob \
        bleak \
        boto3 \
        boto3_type_annotations \
        cbor==1.0.0 \
        click \
        cmake \
        gcovr \
        gitlint \
        greentea-host \
        imgtool \
        in_place \
        jinja2 \
        jmespath \
        jq \
        jsonschema~=3.0 \
        lavacli~=1.0 \
        mbed-cli \
        mbed-greentea \
        mbed-host-tests \
        mbed-ls \
        pre-commit \
        pytest \
        python-gitlab \
        towncrier \
        virtualenv \
    && : # last line

# ------------------------------------------------------------------------------
# Create the user
RUN groupadd -g $USER_GID $USERNAME \
    && useradd -s /bin/bash -u $USER_UID -g $USER_GID -G docker -m $USERNAME \
    && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
    && chmod 0440 /etc/sudoers.d/$USERNAME \
    && chown -R $USERNAME:$USERNAME $HOME \
    && : # last line

# ------------------------------------------------------------------------------
# Install components

# Download and install arm-none-eabi-gcc
RUN set -x \
    && wget -q  https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.10/gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2 -O - | tar -xj -C /opt \
    && : # last line
ENV PATH=/opt/gcc-arm-none-eabi-10.3-2021.10/bin:$PATH


# Download and install FVP Corstone 300
RUN set -x \
    && wget -q https://developer.arm.com/-/media/Arm%20Developer%20Community/Downloads/OSS/FVP/Corstone-300/FVP_Corstone_SSE-300_11.16_26.tgz \
    && tar -xzf FVP_Corstone_SSE-300_11.16_26.tgz \
    && ./FVP_Corstone_SSE-300.sh --i-agree-to-the-contained-eula -d /opt/FVP_Corstone_SSE-300 -f --no-interactive \
    && rm -r FVP_Corstone_SSE-300_11.16_26.tgz FVP_Corstone_SSE-300.sh license_terms \
    && : # last line
ENV PATH=/opt/FVP_Corstone_SSE-300/models/Linux64_GCC-6.4/:$PATH

# ------------------------------------------------------------------------------
# Install mbed-fastmodel-agent
COPY fm_agent_settings.json $HOME
RUN set -x \
    && mkdir -p /opt/mbed-fastmodel-agent \
    && git clone https://github.com/ARMmbed/mbed-fastmodel-agent.git /opt/mbed-fastmodel-agent\
    && cd /opt/mbed-fastmodel-agent \
    && git checkout v2.0.5 \
    && mv $HOME/fm_agent_settings.json fm_agent/settings.json \
    && python -m pip install . \
    && : # last line

ENV FM_AGENT_CONFIG=/opt/mbed-fastmodel-agent/fm_agent/configs

# ------------------------------------------------------------------------------
# Display, check and save environment settings
# NOTE: using bash instead of Ubuntu default dash due to pipefall being unsupported
# Pipefail is crucial here, if the tools didn't install properly, docker build should not pass because of piping
RUN /bin/bash -c \
   "set -x -o pipefail \
    && python --version 2>&1 | tee env_settings \
    && python3 --version 2>&1 | tee env_settings \
    && git --version | tee -a env_settings \
    && arm-none-eabi-gcc --version | grep arm-none-eabi-gcc | tee -a env_settings \
    && cmake --version | grep version | tee -a env_settings \
    && pytest --version | tee -a env_settings \
    && pre-commit --version | tee -a env_settings \
    && (echo -n 'ninja ' && ninja --version) | tee -a env_settings \
    && (echo -n 'mbed-cli ' && mbed --version) | tee -a env_settings \
    && (echo -n 'mbed-greentea ' && mbedgt --version | grep ^[0-9]) | tee -a env_settings \
    && (echo -n 'mbed-host-tests ' && mbedhtrun --version) | tee -a env_settings \
    && (echo -n 'mbed-ls ' && mbedls --version | grep ^[0-9]) | tee -a env_settings \
    && (echo -n 'mbedfm ' && mbedfm --version) | tee -a env_settings \
    && (echo -n 'FVP_Corstone_SSE-300_Ethos-U55 ' && FVP_Corstone_SSE-300_Ethos-U55 --version | grep 'Fast Models') | tee -a env_settings \
    && (echo -n 'FVP_Corstone_SSE-300_Ethos-U65 ' && FVP_Corstone_SSE-300_Ethos-U65 --version | grep 'Fast Models') | tee -a env_settings \
    && : # LAST LINE"

# ------------------------------------------------------------------------------
# Download and configure open cmsis toolbox, and yq YAML JSON Converter
RUN mkdir -p /usr/local/bin/cmsis_toolbox \
    && mkdir -p /workspaces/cmsis_toolbox
RUN set -x \
      && wget -q https://github.com/Open-CMSIS-Pack/cmsis-toolbox/releases/download/1.5.0/cmsis-toolbox-linux-amd64.tar.gz -O - | tar -xz -C /workspaces/cmsis_toolbox  \
      && cp -r /workspaces/cmsis_toolbox/cmsis-toolbox-linux-amd64/* /usr/local/bin/cmsis_toolbox \
      && wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 \
      && : # last line

RUN chmod a+x /usr/local/bin/yq
ENV PATH="/usr/local/bin/cmsis_toolbox/bin:${PATH}"
RUN cpackget init --pack-root /usr/local/bin/cmsis_toolbox/pack https://www.keil.com/pack/index.pidx
RUN chmod -R a+rwx /usr/local/bin/cmsis_toolbox
ENV CMSIS_PACK_ROOT="/usr/local/bin/cmsis_toolbox/pack"
ENV CMSIS_COMPILER_ROOT="/usr/local/bin/cmsis_toolbox/etc"

# To address permission issue with creating the dependency pyasn1 for example tf-m-firmware-update-on-rtx and tf-m-crypto-on-rtx within CI
RUN mkdir -p $HOME/.local/lib \
    && chown -R $USERNAME:$USERNAME $HOME/.local

# ------------------------------------------------------------------------------
# [Optional] Set the default user. Omit if you want to keep the default as root.
USER $USERNAME
